﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Top;
using Top.Api;
using Top.Api.Response;
using Top.Api.Request;
using System.Text;
using DBControl;
///使用AJAX绑定淘宝类目，在BUY.aspx页面实用.
///胡俊凯2011-07-13
namespace ProjectX
{
    /// <summary>
    /// BindCit 的摘要说明
    /// </summary>
    public class BindCit : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            StringBuilder result = new StringBuilder();
            try
            {
                ITopClient itop = new DefaultTopClient(SystemLib.appurl, SystemLib.appkey, SystemLib.appsecret);
                if (context.Request.Form["cid"].Equals("0"))
                {
                    context.Response.Write("");
                    context.Response.End();
                }

                ItemcatsGetRequest itemcats = new ItemcatsGetRequest();
                itemcats.Fields = "cid,parent_cid,name,is_parent,status";
                itemcats.Cids = context.Request.Form["cid"];
                ItemcatsGetResponse itemcatsresponse = itop.Execute(itemcats);
                if (itemcatsresponse.ItemCats[0].IsParent)
                {
                    itemcats.Cids = "";
                    itemcats.ParentCid = long.Parse(context.Request.Form["cid"]);
                    ItemcatsGetResponse icgr = itop.Execute(itemcats);
                    foreach (var item in icgr.ItemCats)
                    {
                        if (item.Status.Equals("normal"))
                        {
                            if (item.IsParent)
                            {
                                result.Append("<option value=" + item.Cid + ">" + item.Name + "</option>");
                            }
                            else
                            {
                                result.Append("<option value=" + item.Cid + ">" + item.Name + "</option>");
                            }

                        }
                    }
                }
                else
                {
                    ItempropsGetRequest itemprop = new ItempropsGetRequest();
                    itemprop.Fields = "pid,name,prop_values,status";
                    itemprop.Cid = long.Parse(context.Request.Form["cid"]);
                    itemprop.IsKeyProp = true;
                    itemprop.IsEnumProp = true;
                    itemprop.ParentPid = 0;
                    ItempropsGetResponse itemprop_response = itop.Execute(itemprop);
                    if (itemprop_response.ItemProps.Count > 0)
                    {
                        ItempropvaluesGetRequest itemprop_values = new ItempropvaluesGetRequest();
                        itemprop_values.Cid = long.Parse(context.Request.Form["cid"]);
                        itemprop_values.Fields = "name,status,pid";
                        foreach (var item in itemprop_response.ItemProps)
                        {
                            itemprop_values.Pvs = item.Pid.ToString();
                            ItempropvaluesGetResponse itemvalue_response = itop.Execute(itemprop_values);
                            foreach (var childitem in itemvalue_response.PropValues)
                            {
                                if (childitem.Status.Equals("normal"))
                                {
                                    result.Append("<option value=" + childitem.Cid + ">" + childitem.Name + "</option>");
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Append(ex.Message);
            }
            context.Response.Write(result.ToString());
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}